package com.sphd.beautysalon.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.JiFenGiveInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.ConsumeJifenPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.ConsumeJifenView;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConsumeJifenActivity extends BaseActivity implements ConsumeJifenView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.text_chongzhi)
    TextView textChongzhi;
    @BindView(R.id.is_consumegetjifen_switch)
    Switch isConsumegetjifenSwitch;
    @BindView(R.id.restaurant_pay_up_layout)
    RelativeLayout restaurantPayUpLayout;
    @BindView(R.id.money_edit)
    MaterialEditText moneyEdit;
    @BindView(R.id.money_getjifen_edit)
    MaterialEditText moneyGetjifenEdit;
    @BindView(R.id.rechage_consume_edit)
    MaterialEditText rechageConsumeEdit;
    @BindView(R.id.rechage_consume_getjifen_edit)
    MaterialEditText rechageConsumeGetjifenEdit;
    @BindView(R.id.btn_over)
    Button btnOver;
    @BindView(R.id.edit_layout)
    LinearLayout editLayout;
    @BindView(R.id.content_consume_jifen)
    LinearLayout contentConsumeJifen;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    ConsumeJifenPresenterImpl consumeJifenPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consume_jifen);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            setResult(0); finish();
        });
        isConsumegetjifenSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (!b){
                    consumeJifenPresenter.setUirl(RestaurantUrl.Httptitle +
                            RestaurantUrl.ADD_INTEGRAL_SPEND+"cgid="+restaurantInfo.getId()+"&status=0");
                }
                setSwitch();
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        isConsumegetjifenSwitch.setChecked(restaurantInfo.getCg_integral_spend() == 1 ? true : false);
        consumeJifenPresenter = new ConsumeJifenPresenterImpl(this);
        setTitleBar(toolbar,"消费积分设置");
        setSwitch();
    }

    private void setSwitch() {
        if (isConsumegetjifenSwitch.isChecked()) {
            editLayout.setVisibility(View.VISIBLE);
        } else {
            editLayout.setVisibility(View.GONE);
        }
            addData();
    }

    private void addData() {
        consumeJifenPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.TOPUP_SPEND_INTEGRAL + "cgid=" + restaurantInfo.getId() + "&type=1");
    }

    @OnClick(R.id.btn_over)
    public void onClick() {
        checkInputNum();

    }
    /**
     * 检测数据是否为空
     */
    private void checkInputNum() {
        if ((!TextUtils.isEmpty(moneyGetjifenEdit.getText()) && !TextUtils.isEmpty(moneyEdit.getText()))
                || (!TextUtils.isEmpty(rechageConsumeGetjifenEdit.getText()) && !TextUtils.isEmpty(rechageConsumeEdit.getText()))
                || (!TextUtils.isEmpty(moneyGetjifenEdit.getText()) && !TextUtils.isEmpty(moneyEdit.getText())
                && !TextUtils.isEmpty(rechageConsumeGetjifenEdit.getText()) && !TextUtils.isEmpty(rechageConsumeEdit.getText()))) {

            consumeJifenPresenter.setUirl(RestaurantUrl.Httptitle +
                    RestaurantUrl.ADD_INTEGRAL_SPEND+"cgid="+restaurantInfo.getId()+"&status=1"
                    +"&money_spend="+ moneyEdit.getText().toString()
            +"&money_integral="+moneyGetjifenEdit.getText().toString()
            +"&topup_spend="+rechageConsumeEdit.getText().toString()
            +"&topup_integral="+rechageConsumeGetjifenEdit.getText().toString());



        } else {
            if (TextUtils.isEmpty(moneyEdit.getText()))
                moneyEdit.setError("输入错误");
            if (TextUtils.isEmpty(moneyGetjifenEdit.getText()))
                moneyGetjifenEdit.setError("输入错误");
            if (TextUtils.isEmpty(rechageConsumeEdit.getText()))
                rechageConsumeEdit.setError("输入错误");
            if (TextUtils.isEmpty(rechageConsumeGetjifenEdit.getText()))
                rechageConsumeGetjifenEdit.setError("输入错误");
        }

    }
    @Override
    public void recList(List<JiFenGiveInfo> jiFenGiveInfoList) {
        if (jiFenGiveInfoList.size()!=0){
            moneyEdit.setText(String.valueOf(jiFenGiveInfoList.get(0).getMoney()));
            moneyGetjifenEdit.setText(String.valueOf(jiFenGiveInfoList.get(0).getIntegral()));
            rechageConsumeEdit.setText(String.valueOf(jiFenGiveInfoList.get(0).getMoney()));
            rechageConsumeGetjifenEdit.setText(String.valueOf(jiFenGiveInfoList.get(0).getIntegral()));

        }
    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("提交成功");
        }else {

            toast("提交失败，请重试！");
        }
        setSwitch();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
